from django.contrib.auth.models import User
from register.models import UserProfile
from django.contrib import admin
from .models import Activity
from django.utils.html import format_html
# Register your models here.

# # 用户信息内联
# class UserInline(admin.TabularInline):
#     model = User

# # 报名管理
# class SignupAdmin(admin.ModelAdmin):
#     inlines = [
        
#     ]

@admin.register(Activity)
class ActivityAdmin(admin.ModelAdmin):
    list_display = ['activity_id','activity_name','undertaker','activity_preview','activity_url','activity_desc','show_participants']
    list_editable = ['activity_name','activity_url','activity_desc']
    search_fields = ['activity_name','undertaker']
    list_display_links = ['activity_id']
    readonly_fields = ('activity_preview',)
    def show_participants(self,obj):
        def myFun(obj):
            one_html = '''
            <table border="1">
                <tr>
                    <th>姓名</th>
                    <th>学号</th>
                    <th>手机</th>
                    <th>邮箱</th>
                </tr>
            '''
            for person in obj.participants.all():
                user = User.objects.get(id = person.id)
                userProfile = UserProfile.objects.get(user = person.id)
                one_html += """
                <tr>
                    <td>{0}{1}</td>
                    <td>{2}</td>
                    <td>{3}</td>
                    <td>{4}</td>
                </tr>
                
                """.format(
                    user.last_name,user.first_name,
                    userProfile.student_id,
                    userProfile.phone,
                    user.email,
                )
            one_html += '</table>'
            return format_html(one_html)
        return myFun(obj)
    show_participants.short_description = "参与者"